import pandas as pd

from quantification.strategy.indicator.Indicator import IndicatorHandler
from service.stock.common import LeastSquareMethod, BaseToolUtil, ApplicationUtil, LineBonding


class TrendIndicators(IndicatorHandler):
    '''
    趋势类型指标
    '''

    def __init__(self, indicatorHandler: IndicatorHandler = None):
        self.next = indicatorHandler

    def handle(self, pd_data):
        '''
        趋势处理（上升，下降，振荡）
        :param pd_data:
        :return:
        '''
        self.trend_judgment(pd_data)
        if self.next:
            self.next.handle(pd_data)

    def trend_judgment(self, pd_data, period=30):
        '''
        K线趋势判断
        :param pd_data:
        :param period: 计算周期，默认最近30日的趋势
        :return:
        '''

        pd_data = LeastSquareMethod.judge_position1(pd_data.tail(2 * period), 'close')


    def support_resistance_position(self):
        '''
        计算阻力位和支撑位
        https://github.com/WestXu/support_resistance_line
        http://pdf.dfcfw.com/pdf/H3_AP201812271279802490_1.pdf
        https://jishuin.proginn.com/p/763bfbd617d6
        https://blog.csdn.net/BF02jgtRS00XKtCx/article/details/119283495
        :return:
        '''

    def gold_dead_cross(self, pd_data, period_short=5, period_long=10):
        '''
        金叉,死叉(MA)
        '''
        CLOSE = pd_data['close']
        pd_data_copy = pd_data.copy()
        ma_5 = BaseToolUtil.MA(CLOSE, period_short)
        ma_10 = BaseToolUtil.MA(CLOSE, period_long)
        # pd_read_csv.loc[:, 'ma_5'] = ma_5
        # pd_read_csv.loc[:, 'ma_10'] = ma_10
        gold_cross = ApplicationUtil.CROSS(ma_5, ma_10)
        death_cross = ApplicationUtil.CROSS(ma_10, ma_5)
        pd_data_copy.loc[:, 'gold_cross'] = gold_cross
        pd_data_copy.loc[:, 'death_cross'] = death_cross
        pd_data_copy.loc[pd_data_copy['ma_gold_cross'] == True, 'cross'] = 1
        pd_data_copy.loc[pd_data_copy['ma_death_cross'] == True, 'cross'] = -1
        pd_data.join(pd_data_copy[['ma_gold_cross', 'ma_death_cross']])

    def altogether(self):
        '''
        判断多条均线是否是胶着一起的
        https://www.toutiao.com/article/6811117422003618311/?&source=m_redirect&wid=1673717058479
        :return:
        '''

    def breach(self,pd_data):
        '''
        突破日的判断
        :return:
        '''
        pd_read_csv=LineBonding().breach1(pd_data)
        pd_read_csv.loc[(pd_read_csv['突破1'] == 1), 'signal'] = 1

    def deviation(self):
        '''
        成交量顶底背离判断
        :return:
        '''
        pass


if __name__ == '__main__':
    pd_read_csv = pd.read_csv(r'D:\workspace\python\stock-flask\service\requests\datas\000868.csv', index_col='date')

    TrendIndicators().trend_judgment(pd_read_csv)
